﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

namespace FYPHP
{
    public partial class MemberSignUpReport : System.Web.UI.Page
    {

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                DropDownList1.Items.Add("UserName");
                DropDownList1.Items.Add("Age");
                DropDownList1.Items.Add("Gender");
                DropDownList1.Items.Add("SignUp Date");

                SqlCommand cmdReport;
                SqlDataReader dtrReport;
                conn.Open();


                cmdReport = new SqlCommand("SELECT CONVERT(varchar, u_date, 103) AS udate, u_picture , username , u_age , u_gender,u_id From Users WHERE (u_position='Member' OR u_position='ShopOwner')", conn);
                dtrReport = cmdReport.ExecuteReader();

                ItemList.DataSource = dtrReport;
                ItemList.DataBind();

                dtrReport.Close();
                conn.Close();

                MultiView1.ActiveViewIndex = 0;
                Panel1.Visible = true;
                Panel2.Visible = false;

            }

        }
        
        
        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlCommand cmdReport;
            SqlDataReader dtrReport;
            conn.Open();

            if (RadioButtonList1.SelectedValue == "Daily")
            {
                TextBox1.Text = "";
                TextBox3.Text = "";
                DropDownList1.Items.Add("UserName");
                DropDownList1.Items.Add("Age");
                DropDownList1.Items.Add("Gender");
                DropDownList1.Items.Add("SignUp Date");

                cmdReport = new SqlCommand("SELECT CONVERT(varchar, u_date, 103) AS udate, u_picture , username , u_age , u_gender, u_id From Users WHERE (u_position='Member' OR u_position='ShopOwner')", conn);
                dtrReport = cmdReport.ExecuteReader();

                ItemList.DataSource = dtrReport;
                ItemList.DataBind();

                dtrReport.Close();
                conn.Close();

                MultiView1.ActiveViewIndex = 0;
                Panel1.Visible = true;
                Panel2.Visible = false;
            }
            else if (RadioButtonList1.SelectedValue == "Monthly")
            {
                cmdReport = new SqlCommand("Select DISTINCT(DATEPART(year,u_date)) AS year From Users WHERE (u_position='Member' OR u_position='ShopOwner')", conn);
                dtrReport = cmdReport.ExecuteReader();
                ddlMonth.DataSource = dtrReport;
                ddlMonth.DataTextField = "year";
                ddlMonth.DataBind();
                dtrReport.Close();


                TextBox1.Text = "";
                TextBox3.Text = "";
                DropDownList1.Items.Clear();
                DropDownList1.Items.Add("Month");
                DropDownList1.Items.Add("Total Sign Up");


                cmdReport = new SqlCommand("SELECT DATEPART(year, u_date) AS tyear,  DATENAME(month,u_date) AS tmonth, COUNT(u_id) AS tusers FROM Users WHERE (u_position='Member' OR u_position='ShopOwner') GROUP BY DATEPART(year, u_date), DATENAME(month,u_date)", conn);
                dtrReport = cmdReport.ExecuteReader();

                ItemList2.DataSource = dtrReport;
                ItemList2.DataBind();

                dtrReport.Close();


                MultiView1.ActiveViewIndex = 1;
                Panel1.Visible = false;
                Panel2.Visible = true;

            }
            else if (RadioButtonList1.SelectedValue == "Yearly")
            {

                DropDownList1.Items.Clear();
                DropDownList1.Items.Add("Year");
                DropDownList1.Items.Add("Total Sign Up");

                //SELECT DATEPART(datepart,'2007-10-30 12:15:32.1234567 +05:10'). - 2007 if DATEPART(Year,date)  & DATENAME(month,date)  - October

                //Obtained Year Counter of Transaction and Total of Transaction

                cmdReport = new SqlCommand("SELECT DATEPART(year, u_date) AS tyear, COUNT(u_id) AS tusers FROM Users WHERE (u_position='Member' OR u_position='ShopOwner') GROUP BY DATEPART(year, u_date)", conn);
                dtrReport = cmdReport.ExecuteReader();

                ItemList3.DataSource = dtrReport;
                ItemList3.DataBind();
                dtrReport.Close();
                
                MultiView1.ActiveViewIndex = 2;
                Panel1.Visible = false;
                Panel2.Visible = false;

            }
            conn.Close();

        }

        protected void btnSort_Click(object sender, EventArgs e)
        {
            SqlCommand cmdReport;
            SqlDataReader dtrReport;
            conn.Open();

            String sort = "", sc = "";

            if (CheckBox1.Checked)
                sc = "DESC";


            if (RadioButtonList1.SelectedValue == "Daily")
            {
                if (DropDownList1.SelectedValue == "UserName")
                    sort = "username";
                else if (DropDownList1.SelectedValue == "Age")
                    sort = "u_age";
                else if (DropDownList1.SelectedValue == "Gender")
                    sort = "u_gender";
                else if (DropDownList1.SelectedValue == "SignUp Date")
                    sort = "u_date";

                if (TextBox1.Text != "" && TextBox3.Text != "")
                {
                    cmdReport = new SqlCommand("SELECT CONVERT(varchar, u_date, 103) AS udate, u_picture , username , u_age , u_gender,u_id  From Users WHERE (u_position='Member' OR u_position='ShopOwner') AND CONVERT(DATE, u_date, 103) BETWEEN CONVERT(DATE, @fdate, 103) AND CONVERT(DATE, @tdate, 103) ORDER BY " + sort + " " + sc, conn);
                    cmdReport.Parameters.AddWithValue("@fdate", TextBox1.Text);
                    cmdReport.Parameters.AddWithValue("@tdate", TextBox3.Text);
                }
                else if (TextBox1.Text != "" && TextBox3.Text == "") //If From is not Empty and To is Empty
                {
                    cmdReport = new SqlCommand("SELECT CONVERT(varchar, u_date, 103) AS udate, u_picture , username , u_age , u_gender,u_id  From Users WHERE (u_position='Member' OR u_position='ShopOwner') AND CONVERT(DATE, u_date, 103) >= CONVERT(DATE, @fdate, 103) ORDER BY " + sort + " " + sc, conn);
                    cmdReport.Parameters.AddWithValue("@fdate", TextBox1.Text);
                }
                else if (TextBox1.Text == "" && TextBox3.Text != "")  //If  From is Empty and To is Not Empty
                {

                    cmdReport = new SqlCommand("SELECT CONVERT(varchar, u_date, 103) AS udate, u_picture , username , u_age , u_gender,u_id  From Users WHERE (u_position='Member' OR u_position='ShopOwner') AND CONVERT(DATE, u_date, 103) <= CONVERT(DATE, @tdate, 103) ORDER BY " + sort + " " + sc, conn);
                    cmdReport.Parameters.AddWithValue("@tdate", TextBox3.Text);

                }
                else // If From is Empty and To is Empty
                {
                    cmdReport = new SqlCommand("SELECT CONVERT(varchar, u_date, 103) AS udate, u_picture , username , u_age , u_gender,u_id  From Users WHERE (u_position='Member' OR u_position='ShopOwner') ORDER BY " + sort + " " + sc, conn);
                }

                dtrReport = cmdReport.ExecuteReader();

                ItemList.DataSource = dtrReport;
                ItemList.DataBind();
                dtrReport.Close();

                MultiView1.ActiveViewIndex = 0;

            }
            else if (RadioButtonList1.SelectedValue == "Monthly")
            {
                if (DropDownList1.SelectedValue == "Month")
                    sort = "DATEPART(year, u_date), DATEPART(month, u_date)";
                else if (DropDownList1.SelectedValue == "Total Sign Up")
                    sort = "tusers";


                cmdReport = new SqlCommand("SELECT DATEPART(year, u_date) AS tyear, DATEPART(month, u_date) AS ttmonth, DATENAME(month, u_date) AS tmonth, COUNT(u_id) AS tusers FROM Users WHERE (u_position='Member' OR u_position='ShopOwner') AND DATEPART(year, u_date)=@tdate GROUP BY DATEPART(year, u_date), DATEPART(month, u_date), DATENAME(month, u_date) ORDER BY " + sort + " " + sc, conn);
                cmdReport.Parameters.AddWithValue("@tdate", ddlMonth.SelectedValue);
                dtrReport = cmdReport.ExecuteReader();

                ItemList2.DataSource = dtrReport;
                ItemList2.DataBind();
                dtrReport.Close();

                MultiView1.ActiveViewIndex = 1;
            }
            else if (RadioButtonList1.SelectedValue == "Yearly")
            {
                if (DropDownList1.SelectedValue == "Year")
                    sort = "DATEPART(year, u_date)";
                else if (DropDownList1.SelectedValue == "Total Sign Up")
                    sort = "tusers";

              

                //Obtained Year, Counter of Transaction ttrans, Total of Transaction t_total

                cmdReport = new SqlCommand("SELECT DATEPART(year, u_date) AS tyear, COUNT(u_id) AS tusers FROM Users WHERE (u_position='Member' OR u_position='ShopOwner') GROUP BY DATEPART(year, u_date) ORDER BY " + sort + " " + sc, conn);
                dtrReport = cmdReport.ExecuteReader();

                ItemList3.DataSource = dtrReport;
                ItemList3.DataBind();
                dtrReport.Close();

              
                MultiView1.ActiveViewIndex = 2;
            }


            conn.Close();

        }


        protected void ItemList_ItemCommand(Object Sender, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "View")
            {
                int uid = Convert.ToInt32(e.CommandArgument.ToString());
                Redirect("../ViewMemberProfileViaReport.aspx?uid=" + uid);
            }
        }


        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        private void Redirect(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language=\"javascript\">window.open('" + msg + "', '_blank');</script>";
            Page.Controls.Add(lbl);
        }


    }
}